package com.lenovo.manufacture.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.lenovo.manufacture.R;
import com.lenovo.manufacture.fragment.DingZhiFragment;
import com.lenovo.manufacture.fragment.GuanLiFragment;

import java.util.ArrayList;
import java.util.List;

public class DingZhiActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private Button ivBack;
    private TextView tvTitle;
    private RadioButton rbDzDz;
    private RadioButton rbDzGl;
    private ViewPager vpDzPager;
    private List<Fragment> list = new ArrayList<>();
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ding_zhi);
        initView();
    }

    private void initView() {
        ivBack = (Button) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        rbDzDz = (RadioButton) findViewById(R.id.rb_dz_dz);
        rbDzGl = (RadioButton) findViewById(R.id.rb_dz_gl);
        vpDzPager = (ViewPager) findViewById(R.id.vp_dz_pager);

        tvTitle.setText("车辆定制");
        ivBack.setOnClickListener(this);
        rbDzDz.setOnClickListener(this);
        rbDzGl.setOnClickListener(this);
        vpDzPager.addOnPageChangeListener(this);

        list.add(new DingZhiFragment());
        list.add(new GuanLiFragment());

        FragmentManager manager = getSupportFragmentManager();
        myAdapter = new MyAdapter(manager);
        vpDzPager.setAdapter(myAdapter);
        vpDzPager.setCurrentItem(0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rb_dz_dz:
                vpDzPager.setCurrentItem(0);
                break;
            case R.id.rb_dz_gl:
                vpDzPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            rbDzDz.toggle();
        } else {
            rbDzGl.toggle();
        }

        myAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
